//
//  MoreMiddleViewController.m
//  SYiDaiy
//
//  Created by dllo on 16/9/8.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MoreMiddleViewController.h"
#import "TAMacro.h"
#import "MoreMiddleCollectionViewCell.h"
#import "AFNetworking.h"
#import "BrandTopViewController.h"
#import "MoreESRootClass.h"
#import "MoreBanner_Brand.h"
#import "MoreDetailViewController.h"
#import "MoreCollectionReusableView.h"
@interface MoreMiddleViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) UICollectionViewFlowLayout *flow;
@property (nonatomic,strong) MoreESRootClass *moreESRootClass;
@property (nonatomic,strong) NSArray *reuseArray;
@end

@implementation MoreMiddleViewController
#pragma mark -------moreCollection视图的加载
- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatCollectionView];
    _reuseArray = @[@"推荐应用 Soecial Honored",@"生活 Lifestyle",@"效率 Productivity",@"深度阅读 Focus"];
}

- (void)creatCollectionView
{
    self.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, 1500);
    _flow = [UICollectionViewFlowLayout new];
    CGFloat width = (SCREEN_WIDTH - 65) / 4;
    _flow.itemSize = CGSizeMake(width, width + 40);
    _flow.minimumLineSpacing = 10;
    _flow.minimumInteritemSpacing = 15;
    _flow.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    _flow.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 2);
    _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:_flow];
    [self.collectionView reloadData];
    [self moreNetworkGet];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    
    [_collectionView registerClass:[MoreMiddleCollectionViewCell class] forCellWithReuseIdentifier:@"collectionViewCell"];
    
    [_collectionView registerClass:[MoreCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"middleReuse"];
    
}

#pragma mark ---返回cell的方法
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row;
    if (indexPath.section == 0) {
        row = indexPath.row + 4;
    } else if (indexPath.section == 1) {
        row = indexPath.row + 8;
    } else if (indexPath.section == 2) {
        row = indexPath.row + 18;
    } else {
        row = indexPath.row + 28;
    }
    MoreMiddleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionViewCell" forIndexPath:indexPath];
    cell.nameLabel.text = _moreESRootClass.apps[row].name;
    cell.descLabel.text = _moreESRootClass.apps[row].desc;
    [cell.iconImageView sd_setImageWithURL:[NSURL URLWithString:_moreESRootClass.apps[row].icon]];
    return cell;
}
#pragma mark ----cell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 4;
    } else if (section == 1) {
        return 10;
    } else if (section == 2) {
        return 10;
    }
    return 9;
}
#pragma mark ----分区个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 4;
}
#pragma mark ------集合视图的头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    MoreCollectionReusableView *reuse = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"middleReuse" forIndexPath:indexPath];
    reuse.backgroundColor = [UIColor colorWithWhite:0.982 alpha:1.000];

//    reuse.moreLabel.text = _reuseArray[indexPath.row];
    reuse.moreLabel.textColor = [UIColor colorWithWhite:0.865 alpha:1.000];
    return reuse;
}
#pragma mark -----网络请求
- (void)moreNetworkGet
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *requestURLString = @"http://watch-cdn.idailywatch.com/app/watch/honored?ver=iphone&app_ver=15&lang=zh-Hans-CN";
    
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.requestSerializer.timeoutInterval = 0;
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager GET:requestURLString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSDictionary dictionary];
        dic = responseObject;
        _moreESRootClass = [MoreESRootClass modelWithDic:dic];
        
        [self.collectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",task.currentRequest);
        NSLog(@"请求失败%@",error);
    }];
}

#pragma mark --------item的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MoreDetailViewController *moreDetailVC = [MoreDetailViewController new];
    if (indexPath.section == 0) {
        
    moreDetailVC.url = _moreESRootClass.apps[indexPath.row + 4].url;
    } else if (indexPath.section == 1) {
        moreDetailVC.url = _moreESRootClass.apps[indexPath.row + 8].url;
    } else if (indexPath.section == 2) {
        moreDetailVC.url = _moreESRootClass.apps[indexPath.row + 18].url;
    } else {
        moreDetailVC.url = _moreESRootClass.apps[indexPath.row + 28].url;
    }
    [self.navigationController pushViewController:moreDetailVC animated:YES];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}


@end
